package com.sdry.web.controller.hpf;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sdry.model.hpf.LineSideLibrary;
import com.sdry.model.hpf.Page;
import com.sdry.service.hpf.LineSideLibraryService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * @ClassName: LineSideLibraryController 
 * @Description:线边库Controller
 * @Author hpf
 * @Date 2018年9月19日 上午11:44:31 
 * @version 1.0
 */
@Controller
@RequestMapping(value="/lineSideLibrary")
public class LineSideLibraryController {

	@Resource
	private LineSideLibraryService lineSideLibraryService;
	
	/**
	 * 分页查询线边库
	 * @param page 分页条件
	 * @param lineSideLibrary 查询条件
	 * @param response
	 */
	@RequestMapping(value="/listPageLineSideLibrary")
	public void listPageLineSideLibrary(Page page,LineSideLibrary lineSideLibrary,HttpServletResponse response){
		Map<String, Object> map = new HashMap<>();
		map.put("page", page.getPage());
		map.put("limit", page.getLimit());
		if (lineSideLibrary.getPanum() != null) {
			map.put("panum", lineSideLibrary.getPanum());
		} else {
			map.put("panum", "");
			lineSideLibrary.setPanum("");
		}
		List<LineSideLibrary> lineSideLibraryList = lineSideLibraryService.listPageLineSideLibrary(map);
		int count = lineSideLibraryService.countLineSideLibrary(lineSideLibrary);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(lineSideLibraryList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 设置预警值
	 * @param lineSideLibrary 线边库实体
	 * @return
	 */
	@RequestMapping(value="/setWarningValue2")
	public void setWarningValue(LineSideLibrary lineSideLibrary,HttpServletResponse response){
		int isSuccess = lineSideLibraryService.setWarningValue(lineSideLibrary);
		try {
			ResponseUtil.write(response, isSuccess);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 线边库叫料
	 * @param paID 配件ID
	 * @param batch 批次号
	 * @param quantity 数量
	 * @param response
	 * @param session
	 */
	@RequestMapping(value="/lineSideLibraryIn")
	public void lineSideStockIn(Long[] paID,String[] batch,String[] quantity,HttpServletResponse response,HttpSession session){
		int isSuccess = 0;
		isSuccess = lineSideLibraryService.insertLineSideLibrary(paID, batch, quantity, session);
		try {
			ResponseUtil.write(response, isSuccess);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 线边库出库
	 * @param pnum 产品编码
	 * @param quantity 数量
	 * @param productionOrder 排产单号
	 * @param team 班组名称
	 * @param type 1合格2报废3让步接收4降级
	 * @param positionName 工位名称
	 * @param response
	 */
	@RequestMapping(value="/lineSideLibraryOut")
	public void lineSideLibraryOut(String pnum,String quantity,String productionOrder,String team,String type,HttpServletResponse response, HttpSession session, String positionName){
//		lineSideLibraryService.lineSideLibraryOut(pnum, quantity, productionOrder, team, type, session);
		lineSideLibraryService.newLineSideLibraryOut(pnum, quantity, productionOrder, team, type, session, positionName);
	}
	
	/**
	 * 线边库盘点
	 * @param list
	 * @param response
	 */
	@RequestMapping(value="/saveLineSideLibrary")
	public void saveLineSideLibrary(String list,HttpServletResponse response){
		int isSuccess = 0;
		JSONArray ary = JSONArray.fromObject(list);
		if(ary != null && ary.size() > 0){
            List<LineSideLibrary> lineSideLibraryList = (List<LineSideLibrary>)JSONArray.toCollection(ary, 
                    LineSideLibrary.class);
            isSuccess = lineSideLibraryService.lineSideLibraryCheck(lineSideLibraryList);
        }
		try {
			ResponseUtil.write(response, isSuccess);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 线边库盘点清空
	 * @param list
	 * @param response
	 */
	@RequestMapping(value="/saveLineSideLibrary2")
	public void saveLineSideLibrary2(String list,HttpServletResponse response){
		int isSuccess = 0;
		JSONArray ary = JSONArray.fromObject(list);
		if(ary != null && ary.size() > 0){
			List<LineSideLibrary> lineSideLibraryList = (List<LineSideLibrary>)JSONArray.toCollection(ary, 
					LineSideLibrary.class);
			isSuccess = lineSideLibraryService.lineSideLibraryEmpty(lineSideLibraryList);
		}
		try {
			ResponseUtil.write(response, isSuccess);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
